package org.linlinjava.litemall.db.service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import org.linlinjava.litemall.db.dao.LitemallAccountLogMapper;
import org.linlinjava.litemall.db.domain.LitemallAccountLog;
import org.linlinjava.litemall.db.domain.LitemallAccountLogExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.github.pagehelper.PageHelper;

@Service
public class LitemallAccountLogService {
  @Resource
  private LitemallAccountLogMapper accountLogMapper;

  public LitemallAccountLog findById(Integer id) {
    return accountLogMapper.selectByPrimaryKey(id);
  }

  @Transactional
  private LitemallAccountLog add(//
      Integer accountId, String type, //
      String remark, String orderNo, //
      BigDecimal amount, //
      BigDecimal afterBalance//
  ) {
    LitemallAccountLog log = new LitemallAccountLog();
    log.setUpdateTime(LocalDateTime.now());
    log.setCreateTime(LocalDateTime.now());
    log.setAccountId(accountId);
    log.setType(type);
    log.setRemark(remark);
    log.setOrderNo(orderNo);
    log.setAmount(amount);
    log.setAfterBalance(afterBalance);

    accountLogMapper.insertSelective(log);
    return log;
  }

  @Transactional
  LitemallAccountLog withdraw(//
      Integer accountId, String remark, String orderNo, BigDecimal amount, BigDecimal afterBalance//
  ) {
    String type = "提现";
    return add(accountId, type, remark, orderNo, amount, afterBalance);
  }

  @Transactional
  LitemallAccountLog recharge(//
      Integer accountId, String remark, String orderNo, BigDecimal amount, BigDecimal afterBalance//
  ) {
    String type = "充值";
    return add(accountId, type, remark, orderNo, amount, afterBalance);
  }

  @Transactional
  LitemallAccountLog consume(//
      Integer accountId, String remark, String orderNo, BigDecimal amount, BigDecimal afterBalance//
  ) {
    String type = "消费";
    return add(accountId, type, remark, orderNo, amount, afterBalance);
  }

  @Transactional
  LitemallAccountLog refund(//
      Integer accountId, String remark, String orderNo, BigDecimal amount, BigDecimal afterBalance//
  ) {
    String type = "退款";
    return add(accountId, type, remark, orderNo, amount, afterBalance);
  }


  public List<LitemallAccountLog> querySelective(Integer accountId, LocalDateTime start,
      LocalDateTime end, Integer page, Integer size, String sort, String order) {
    LitemallAccountLogExample example = new LitemallAccountLogExample();
    LitemallAccountLogExample.Criteria criteria = example.createCriteria();

    if (accountId != null) {
      criteria.andAccountIdEqualTo(accountId);
    }
    if (start != null) {
      criteria.andCreateTimeGreaterThanOrEqualTo(start);
    }
    if (end != null) {
      criteria.andCreateTimeLessThanOrEqualTo(end);
    }

    if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
      example.setOrderByClause(sort + " " + order);
    }

    PageHelper.startPage(page, size);
    return accountLogMapper.selectByExample(example);
  }

}
